﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Lime49.UI;
using LockCrypt.Core;

namespace LockCrypt {
    /// <summary>
    /// A window for editing wildcard field filters.
    /// </summary>
    public partial class WinAddFieldFilter : Window {
        /// <summary>
        /// Gets or sets the current field wildcard.
        /// </summary>
        /// <value>The current field wildcard.</value>
        public WildcardField CurrentField {
            get {
                var newField = new WildcardField();
                if(txtFilterFieldName.Text.Length > 0) {
                    WildcardValue.WildcardTypes fieldNameType;
                    switch (cboFilterFieldName.SelectedIndex) {
                        case 1:
                            fieldNameType = WildcardValue.WildcardTypes.StartsWith;
                            break;
                        case 2:
                            fieldNameType = WildcardValue.WildcardTypes.EndsWith;
                            break;
                        case 0:
                        default:
                            fieldNameType = WildcardValue.WildcardTypes.Contains;
                            break;
                    }
                    newField.FieldName = new WildcardValue(txtFilterFieldName.Text, fieldNameType);
                }
                if(txtFilterFieldValue.Text.Length > 0) {
                    WildcardValue.WildcardTypes fieldValueType;
                    switch (cboFilterFieldValue.SelectedIndex) {
                        case 1:
                            fieldValueType = WildcardValue.WildcardTypes.StartsWith;
                            break;
                        case 2:
                            fieldValueType = WildcardValue.WildcardTypes.EndsWith;
                            break;
                        case 0:
                        default:
                            fieldValueType = WildcardValue.WildcardTypes.Contains;
                            break;
                    }
                    newField.FieldValue = new WildcardValue(txtFilterFieldValue.Text, fieldValueType);
                }
                return newField;
            }
            set {
                if(value.FieldName != null) {
                    txtFilterFieldName.Text = value.FieldName.Value;
                    cboFilterFieldName.SelectedIndex = (int)value.FieldName.WildcardType;
                } else if(cboFilterFieldName.HasItems) {
                    cboFilterFieldName.SelectedIndex = 0;
                }
                if(value.FieldValue != null) {
                    txtFilterFieldValue.Text = value.FieldValue.Value;
                    cboFilterFieldValue.SelectedIndex = (int)value.FieldValue.WildcardType;
                } else if(cboFilterFieldValue.HasItems) {
                    cboFilterFieldValue.SelectedIndex = 0;
                }
            }
        }

        /// <summary>
        /// Initializes a new <see cref="WinAddFieldFilter"/> to add a field wildcard.
        /// </summary>
        public WinAddFieldFilter() {
            InitializeComponent();
            CurrentField = new WildcardField();
        }

        /// <summary>
        /// Initializes a new <see cref="WinAddFieldFilter"/> to edit a field wildcard.
        /// </summary>
        /// <param name="wildcard">The wildcard to edit.</param>
        public WinAddFieldFilter(WildcardField wildcard) 
            :this() {
            this.CurrentField = wildcard;
            Title = I18nUtils.GetString("Strings", "EditField");
            btnSave.Content = I18nUtils.GetString("Strings", "Save");
        }

        /// <summary>
        /// Loads a localized set of 'contains, starts with, ends with'.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e) {
            string[] filters = new[]
                                   {
                                       I18nUtils.GetString("Strings", "Contains"),
                                       I18nUtils.GetString("Strings", "StartsWith"),
                                       I18nUtils.GetString("Strings", "EndsWith"),
                                   };
            var filterTypeSource = this.Resources["filterTypeSource"] as CollectionViewSource;
            filterTypeSource.Source = filters;
            if(cboFilterFieldName.SelectedIndex == -1)
                cboFilterFieldName.SelectedIndex = 0;
            if(cboFilterFieldValue.SelectedIndex == -1)
                cboFilterFieldValue.SelectedIndex = 0;
        }


        /// <summary>
        /// Checks the data entered is valid.
        /// </summary>
        /// <returns><c>True</c> if the data is valid, otherwise <c>false</c></returns>
        private bool Validate() {
            bool valid = true;
            if(txtFilterFieldName.Text.Trim().Length == 0 && txtFilterFieldValue.Text.Trim().Length == 0) {
                DialogBox.ShowAlert(this, I18nUtils.GetString("Errors", "NameValueBothRequired"), I18nUtils.GetString("Errors", "Error"));
                txtFilterFieldName.Focus();
                valid = false;
            }
            return valid;
        }

        /// <summary>
        /// Validates, then returns a dialog result of true.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void Save(object sender, ExecutedRoutedEventArgs e) {
            if(Validate()) {
                DialogResult = true;
            }
        }

        /// <summary>
        /// Closes the window.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void CloseWindow(object sender, ExecutedRoutedEventArgs e) {
            DialogResult = false;
        }
    }
}
